<template>
  <div>


      <ex-edit-table
      ref='exEditTable'
      :tableColumn="tableColumn"
      :tableData="tableData"
      :loading.sync="loading"
      :editRules="editRules"
      @saveRowEventEmit="saveRowEventEmit"
      @removeRowEventEmit="removeRowEventEmit"
      @editRowEventEmit="editRowEventEmit"
      >
      
      </ex-edit-table>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
import XEUtils,{ isNaN } from 'xe-utils'
import { filterAllRowBynormData } from '@/utils/index'
// 基础表格组件
import ExEditTable from '@/components/VxeTable/ExEditTable'
import {
  tCCurveSuperpositionFindTCCurveSuperpositionzi,
  tCCurveSuperpositionFindtermCode,
  addTCCurveSuperpositionzi,
  updateTCCurveSuperpositionzi,
  deleteTCCurveSuperpositionzi
} from '../../service/baseApi'

export default {
  name: 'TimePointsManageTable',
  components: {
    ExEditTable
  },
  props: {
    /* 当前行数据 */
    currentRow: {
      type: Object,
      default() {
        return {}
      }
    },
    // //期限点编号--待
    // stayTermCodeOptions: {
    //   type: Array
    // },
    // //期限点编号--叠
    // sppstTermCodeOptions: {
    //   type: Array
    // },
    // //期限点编号--合
    // mergeTermCodeOptions: {
    //   type: Array
    // },
  },
  // 定义属性
  data() {
    
    return {
      // 表单数据对象
      formSubmitData: {
        id: undefined,
        m_id: "",
        merge_term_code: "",
        rfr_value: "",
        sppst_pct_value: "",
        sppst_term_code: "",
        stay_pct_value: "",
        stay_term_code: "",
      },
      loading:false,

      tableColumn: [
        // { type: 'seq', width: 60 },
        { type: 'checkbox', width: 50 },
        { field: 'id', width: 160,title: 'ID',visible:false },
        { field: 'm_id', width: 160,title: 'MID',visible:false },
        // { field: 'stay_term_code', width: 160,title: '期限点编号-待', editRender: { name: 'select', options: [] } },
        {
          field: 'stay_term_code', width: 160,title: '期限点编号-待', 
          editRender: {
            name: 'ElSelect', 
            options: [] ,
            props: { size: 'small', clearable: true },
          }
        },
        
        // { field: 'sppst_term_code',width: 160, title: '期限点编号-叠', editRender: { name: 'select', options: [] } },
        {
          field: 'sppst_term_code', width: 160,title: '期限点编号-叠', 
          editRender: {
            name: 'ElSelect', 
            options: [] ,
            props: { size: 'small', clearable: true },
          }
        },

        // { field: 'merge_term_code',width: 160, title: '期限点编号-合', editRender: { name: 'select', options: [] } },
        {
          field: 'merge_term_code', width: 160,title: '期限点编号-合', 
          editRender: {
            name: 'ElSelect', 
            options: [] ,
            props: { size: 'small', clearable: true },
          }
        },

        // { field: 'stay_pct_value',width: 160, title: '值-待(%)', editRender: { name: '$input', props: {type: 'number',placeholder: ''} } },
        {
          field: 'stay_pct_value', width: 160,title: '值-待(%)', 
          editRender: {
            name: 'ElInputNumber', 
            options: [] ,
            props: { size: 'small', clearable: true },
          }
        },

        // { field: 'sppst_pct_value',width: 160, title: '值-叠(%)', editRender: { name: '$input', props: {type: 'number',placeholder: ''} } },
        {
          field: 'sppst_pct_value', width: 160,title: '值-叠(%)', 
          editRender: {
            name: 'ElInputNumber', 
            options: [] ,
            props: { size: 'small', clearable: true },
          }
        },

        { width: 160, title: '值--合(%)' ,fixed:'right', slots: { default: ({ row }) => {
                      let re =  Number(row.stay_pct_value) + Number(row.sppst_pct_value)
                      return isNaN(re)? 0 : re 
                    } }},
        // { field: 'rfr_value',width: 160, title: '参照倍数', editRender: { name: '$input', props: {type: 'number',placeholder: ''} } },
        {
          field: 'rfr_value', width: 160,title: '参照倍数', 
          editRender: {
            name: 'ElInputNumber', 
            options: [] ,
            props: { size: 'small', clearable: true },
          }
        },

        { title: '操作',fixed:'right', width: 200, slots: { default: 'operate' } }
      ],
      tableData: [
      ],
      editRules:{
        stay_term_code: [
          { required: true, message: '期限点编号-待,不能为空' },
        ],
        sppst_term_code: [
          { required: true, message: '期限点编号-叠,不能为空' },
        ],
        merge_term_code: [
          { required: true, message: '期限点编号-合,不能为空' },
        ],
        stay_pct_value: [
          { required: true, message: '值-待,不能为空' },
        ],
        sppst_pct_value: [
          { required: true, message: '值-叠,不能为空' },
        ],
        rfr_value: [
          { required: true, message: '参照倍数,不能为空' },
        ],
        
      }




    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
  },
  // 监控data中的数据变化
  watch: {
    currentRow(dataVal, oldVal){
      this.findTCCurveSuperpositionFindTCCurveSuperpositionzi(this.currentRow.id)
      this.findTCCurveSuperpositionFindtermCode(dataVal)
    }
  },
  // 方法集合
  methods: {
    /**
     * 过滤表单提交之外的数据（返回新数组）
     */
    // filterFormSubmitDataExcept(obj){
    //   let f,allRow
    //   if (XEUtils.isEmpty(obj.allRow) || XEUtils.isEmpty(obj.formSubmitData)) {
    //     return []
    //   }
    //   allRow = obj.allRow
    //   f = obj.formSubmitData

    //   XEUtils.objectEach (allRow, (item, key) => {
    //     if (!f.hasOwnProperty(key)) {
    //       delete allRow[`${key}`]
    //     }
    //   })
    //   return allRow
    // },
    async saveRowEventEmit(row){

      if (XEUtils.isEmpty(row.id)) {
        await addTCCurveSuperpositionzi({
          ...row,
          m_id:this.currentRow.id
        }).then(({data}) => {

        }, error => {
          console.log('getList:error', error);
        })

      }else{
        
        // 排除FormSubmitData没有的元素
        row = filterAllRowBynormData({
          allRow: XEUtils.clone(row, true),
          normData:this.formSubmitData
        })
        
        await updateTCCurveSuperpositionzi({
          ...row,
          m_id:this.currentRow.id
        }).then(({data}) => {

        }, error => {
          console.log('getList:error', error);
        })
      }
      

      await this.findTCCurveSuperpositionFindTCCurveSuperpositionzi(this.currentRow.id)

      await this.$emit('handleFindListEmit')

      console.log('saveRowEventEmitsaveRowEventEmit',row);
      console.log('removeRowEventEmitremoveRowEventEmit-mid',this.currentRow.id);
    },
    removeRowEventEmit(row){

      deleteTCCurveSuperpositionzi({
        id:row.id
      }).then(({data}) => {
        this.$refs.exEditTable.$refs.xGrid.remove(row)
      })
      // console.log('removeRowEventEmitremoveRowEventEmit',row);
      // console.log('removeRowEventEmitremoveRowEventEmit-mid',this.currentRow.id);
    },
    /* 点击编辑按钮 */
    editRowEventEmit(row){
      // 当前行开启编辑模式
      this.$refs.exEditTable.$refs.xGrid.setActiveRow(row)
    },

    findTCCurveSuperpositionFindtermCode(row){
      // 待
      tCCurveSuperpositionFindtermCode({
        type:row.stay_type,
        s_date:row.s_date,
        e_date:row.e_date,
        cv_code:row.stay_curve_code
      }).then(({data}) => {
        const stay_term_code_column = this.$refs.exEditTable.$refs.xGrid.getColumnByField('stay_term_code')
        stay_term_code_column.editRender.options = XEUtils.map(data.rows, item =>{
          return {
            label:item.term_name,
            value:item.term_code
          }
        })
      })
      // 叠
      tCCurveSuperpositionFindtermCode({
        type:row.sppst_type,
        s_date:row.s_date,
        e_date:row.e_date,
        cv_code:row.sppst_curve_code
      }).then(({data}) => {

        const merge_term_code_column = this.$refs.exEditTable.$refs.xGrid.getColumnByField('merge_term_code')
        merge_term_code_column.editRender.options = XEUtils.map(data.rows, item =>{
          return {
            label:item.term_name,
            value:item.term_code
          }
        })
      })
      // 合
      tCCurveSuperpositionFindtermCode({
        type:row.merge_type,
        s_date:row.s_date,
        e_date:row.e_date,
        cv_code:row.merge_curve_code
      }).then(({data}) => {
        const sppst_term_code_column = this.$refs.exEditTable.$refs.xGrid.getColumnByField('sppst_term_code')
        sppst_term_code_column.editRender.options = XEUtils.map(data.rows, item =>{
          return {
            label:item.term_name,
            value:item.term_code
          }
        })
      })
    },
    /* 查询 期限字符串列表   参数 m_id */
    findTCCurveSuperpositionFindTCCurveSuperpositionzi(m_id){
      this.loading = true
      tCCurveSuperpositionFindTCCurveSuperpositionzi({
        m_id
      }).then(({data}) => {
        this.tableData = data.rows
        // 关闭加载特效
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      })
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    console.log('currentRowcurrentRow',this.currentRow);
    this.findTCCurveSuperpositionFindTCCurveSuperpositionzi(this.currentRow.id)
    // 获取期限字符串选项
    this.findTCCurveSuperpositionFindtermCode(this.currentRow)

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
        console.log('columncolumncolumnco3lumn',this.$refs.exEditTable);
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
  
</style>